package com.cn.common.topic;

import com.cn.common.config.ConfigManager;
import com.cn.util.GsonUtil;
import com.google.gson.internal.LinkedTreeMap;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * topic管理
 */
public class TopicManager extends ConfigManager {

    private final String path;

    private final ConcurrentMap<String /* topic */, TopicData> topicTables = new ConcurrentHashMap<>();

    public TopicManager(String path) {
        this.path = path;
        if (StringUtils.isNotEmpty(path)) {
            load();
        }
    }

    public TopicData getTopic(String topic) {
        return this.topicTables.get(topic);
    }

    public ConcurrentMap<String /* topic */, TopicData> getAll() {
        return this.topicTables;
    }

    public String getJson() {
        ConcurrentMap<String, ConcurrentMap<String, TopicData>> tables = new ConcurrentHashMap<>();
        tables.put("topic", this.topicTables);
        String result = GsonUtil.converObj2Json(tables);
        return result;
    }

    /**
     * 添加新的topic
     *
     * @param topicData
     */
    public void add(TopicData topicData) {
        this.topicTables.put(topicData.getTopic(), topicData);
    }

    public ConcurrentMap<String /* topic */, TopicData> decode(String content) {
        if (StringUtils.isNotEmpty(content)) {
            Map<String, LinkedTreeMap<String, TopicData>> p1 = new HashMap<>();
            p1 = (Map<String, LinkedTreeMap<String, TopicData>>) GsonUtil.convertJson2Obj(content, p1);
            ConcurrentMap<String /* topic */, TopicData> map = new ConcurrentHashMap<>();
            for (LinkedTreeMap<String, TopicData> p2 : p1.values()) {
                for (String key : p2.keySet()) {
                    Map<String, Object> p3 = (Map<String, Object>) p2.get(key);
                    TopicData t1 = new TopicData();
                    t1.setTopic(p3.get("topic").toString());
                    t1.setReadNum(Double.valueOf(p3.get("readNum").toString()).intValue());
                    t1.setWriteNum(Double.valueOf(p3.get("writeNum").toString()).intValue());
                    map.put(key, t1);
                }
            }
            return map;
        }
        return null;
    }

    public void load() {
        String content = this.readString(this.path);
        ConcurrentMap<String /* topic */, TopicData> topics = decode(content);
        if (topics != null) {
            this.topicTables.putAll(topics);
        }
    }

    public void load(String content) {
        decode(content);
    }

    public void persit() {
        ConcurrentMap<String, ConcurrentMap<String, TopicData>> tables = new ConcurrentHashMap<>();
        tables.put("topic", this.topicTables);
        String result = GsonUtil.converObj2Json(tables);
        string2File(path, result);
    }


    public static void main(String[] args) {
        TopicManager topicManager = new TopicManager("C:\\F\\data3\\data\\rocketmq\\config/topic.json");
        TopicData t1 = new TopicData();
        t1.setTopic("topic").setReadNum(4).setWriteNum(4);
        topicManager.add(t1);

        TopicData t2 = new TopicData();
        t2.setTopic("demo").setReadNum(6).setWriteNum(6);
        topicManager.add(t2);

        topicManager.persit();

//        topicManager.load();
//        System.out.println(topicManager.topicTables);


    }

}
